//
//  MakerAlertView.m
//  ZDPension
//
//  Created by 刘甲奇 on 2017/6/16.
//  Copyright © 2017年 maker. All rights reserved.
//

#import "MakerAlertView.h"

@interface MakerAlertView()

/**
 竖向按钮
 */
@property(nonatomic,strong)NSArray<NSString *> *sheet;
/**
 title
 */
@property(nonatomic,copy)NSString *title;
/**
 白色背景
 */
@property(nonatomic,strong)UIView *whiteView;
/**
 titleLable
 */
@property(nonatomic,strong)UILabel *titleLabel;

@end

@implementation MakerAlertView

+(MakerAlertView *)defautInit
{
    //创建自身视图
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    MakerAlertView *selfView = [[self alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [keyWindow addSubview:selfView];
    selfView.backgroundColor = [UIColor clearColor];
    selfView.alpha=1;
    
    //创建背景图,添加手势
    UIView * backView=[[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [selfView addSubview:backView];
    backView.backgroundColor = [UIColor blackColor];
    backView.alpha = 0.4;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:selfView action:@selector(closeView)];
    [backView addGestureRecognizer:tap];
    
    return selfView;
}

+ (void)showSheetWithTitle:(NSString *)title sheet:(NSArray<NSString *> *)sheet block:(alertClickBlock)block
{
    [[MakerAlertView defautInit]setupUIWithTitle:title sheet:sheet block:block];
}
- (void)setupUIWithTitle:(NSString *)title sheet:(NSArray<NSString *> *)sheet block:(alertClickBlock)block
{
    _title = title;
    _sheet = sheet;
    _clickBlock = block;
    
    //白色背景
    CGFloat padding = 30;
    CGFloat width = LEScreenWidth -2*padding;
    CGFloat height = (50 *sheet.count) + 100;
    
    [self addSubview: self.whiteView];
    self.whiteView.bounds = CGRectMake(0, 0, width, height);
    self.whiteView.center = self.center;
    
    [self.whiteView addSubview:self.titleLabel];
    self.titleLabel.frame = CGRectMake(0, 0, _whiteView.width, 50);
    
    
    


}

#pragma mark- privateMethod
/**
 关闭自身的视图
 */
-(void)closeView
{
    [UIView animateWithDuration:0.3f animations:^{
        
        for (UIView *v in self.subviews) {
            [v removeFromSuperview];
        }
        
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
#pragma mark- getter


/**
 白色背景

 @return <#return value description#>
 */
- (UIView *)whiteView
{
    if (_whiteView == nil)
    {
        _whiteView = [UIView new];
        _whiteView.backgroundColor = [UIColor whiteColor];
    }
    return _whiteView;
}
/**
 titleLabel

 @return <#return value description#>
 */
- (UILabel *)titleLabel
{
    if (_titleLabel == nil)
    {
        _titleLabel = [UILabel new];
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.backgroundColor = NavigationBlueColor;
        _titleLabel.font = [UIFont systemFontOfSize:17];
    }
    return _titleLabel;
}
- (UIButton *)sheetButton
{
    UIButton *sheet = [UIButton new];
    sheet.titleLabel.font = [UIFont systemFontOfSize:15];
    [sheet setTitleColor:[UIColor darkGrayColor] forState:0];
    
    UIImageView *icon = [UIImageView new];
    icon.image = [UIImage imageNamed:@"xz"];
    [sheet addSubview:icon];
    return sheet;
}

@end
